
do "...\First.do"

********************************************************************************
* Death - Finds cause of death
********************************************************************************

clear
use "$raw\patient_dod_moreyears.dta"
keep pnr c_dodtilgrundl_acme d_statdato
rename c_dodtilgrundl_acme c_dod_1a

append using "$raw\patient_dod2_moreyears.dta"

drop c_dod2-c_dod4
replace d_statdato=d_dodsdto if missing(d_statdato)
replace c_dod_1a=c_dod1 if c_dod_1a==""

g yod=year(d_statdato)
keep if yod>1994
tab yod

drop d_dodsdto c_dod1

* Cause of death is coded with ICD-10
g letter=substr(c_dod_1a,1,1)
g number=real(substr(c_dod_1a,2,2))

g death_CVC=(letter=="I")
g death_stroke=(letter=="I" & !inrange(number,0,9))
g death_stroke_acute=(letter=="I" & (inrange(number,20,25) | inrange(number,60,69)))
g death_heart=(letter=="I" & inrange(number,20,25))

g death_cancer=(letter=="C")
g death_cancer_lung=(letter=="C" & number==34)
g death_cancer_mouth=(letter=="C" & inrange(number,0,14))
g death_cancer_digest=(letter=="C" & inrange(number,15,26))
g death_cancer_lungetc=(letter=="C" & inrange(number,30,39))
g death_cancer_bones=(letter=="C" & inrange(number,40,41))
g death_cancer_skin=(letter=="C" & inrange(number,43,44))
g death_cancer_mesotel=(letter=="C" & inrange(number,45,49))
g death_cancer_breast=(letter=="C" & number==50)
g death_cancer_gorgans=(letter=="C" & inrange(number,51,63))
g death_cancer_kidney=(letter=="C" & inrange(number,64,68))
g death_cancer_eyesetc=(letter=="C" & inrange(number,69,72))
g death_cancer_kirtler=(letter=="C" & inrange(number,73,75))
g death_cancer_unspec=(letter=="C" & inrange(number,81,96))

egen rowmax=rowmax(death_cancer_lung-death_cancer_kidney)
g death_cancer_other=(rowmax==0 & death_cancer==1)
drop rowmax

g death_diabetes=(letter=="E" & inrange(number,10,14))
g death_COPD=(letter=="J" & number==44)


keep pnr death_stroke death_stroke_acute yod d_statdato death_heart death_diabetes death_COPD death_CVC death_cancer death_cancer_lung death_cancer_mouth death_cancer_digest death_cancer_lungetc death_cancer_bones death_cancer_skin death_cancer_mesotel death_cancer_breast death_cancer_gorgans death_cancer_kidney death_cancer_eyesetc death_cancer_kirtler death_cancer_unspec death_cancer_other

bys pnr: g N=_N
tab N
drop if N>1 
drop N 
save "$work\deaths.dta", replace

